<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" th:href="@{/layui/css/layui.css}"/>
  <link rel="stylesheet" th:href="@{/css/custom.form.css}">
    <style id="layuimini-bg-color">
    </style>
<body>
<div class="panel panel-default operation" hidden>
  <div class="panel-heading title"></div>
<div class="layui-card-body">
<form class="layui-form " action="" lay-filter="info" style="width: 700px;margin-top: 10px">
      <input name="id" hidden/>
        <div class="layui-form-item">
      <label class="layui-form-label">活动名称</label>
      <div class="layui-input-block">
        <input type="name" name="name" placeholder="请输入活动名称" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">活动开始时间</label>
      <div class="layui-input-block">
          <input type="text" name="startTime" id="startTime" autocomplete="off" placeholder="请输入活动开始时间"
                 class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">活动结束时间</label>
      <div class="layui-input-block">
          <input type="text" name="endTime" id="endTime" autocomplete="off" placeholder="请输入活动结束时间"
                 class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">活动内容描述</label>
      <div class="layui-input-block">
          <textarea placeholder="请输入活动内容描述" name="desr" class="layui-textarea" autocomplete="off" ></textarea>
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">活动总资金</label>
      <div class="layui-input-block">
        <input type="amount" name="amount" placeholder="请输入活动总资金" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">活动所属班级ID</label>
      <div class="layui-input-block">
        <input type="classId" name="classId" placeholder="请输入活动所属班级ID" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">活动类别ID</label>
      <div class="layui-input-block">
        <input type="typeId" name="typeId" placeholder="请输入活动类别ID" autocomplete="off" class="layui-input">
      </div>
    </div>
  <div class="layui-form-item">
  <div class="layui-input-block">
    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
    <button  class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
  </div>
</div>
</form>
</div>
</div>

<div class="table_div">
  <div id="searchParam"  shiro:hasPermission="aActive:list">
    <form class="layui-form ">
      <div class="layui-form-item">
        <div class="layui-input-inline">
          <input type="text" id="searchKeyName" class="layui-input"  autocomplete="off" placeholder="请输入班级名称">
        </div>

        <div class="layui-input-inline ">
          <button class="layui-btn" onclick="searchInfo();return false;"  id="search">查询</button>
<!--          <button class="layui-btn"   id="export">导出全部</button>-->
        </div>
      </div>
    </form>
  </div>
  <table class="layui-table" id="showTable" lay-filter="showTable" ></table>
</div>
<script type="text/html" id="toolbar">
<!--  <div class="layui-btn-container">-->
<!--    <button class="layui-btn layui-btn-sm" lay-event="add"  shiro:hasPermission="aActive:add">添加</button>-->
<!--    <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" shiro:hasPermission="aActive:delete">删除</button>-->
<!--  </div>-->
</script>
<script type="text/html" id="tool">
<!--  <a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="aActive:update">编辑</a>-->
<!--  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="aActive:delete">删除</a>-->
</script>

</body>
</html>
<script th:inline="javascript" type="text/javascript">
  var ctx = '[[@{/}]]'.replaceAll("\"", "").replace('//', '/');
</script>
<script th:src="@{/layui/layui.all.js}"></script>
<script th:src="@{/js/core.util.js}"></script>
<script>
  //获取token
  var token = CoreUtil.getData("access_token");
  //地址栏转义token中的#号
  var tokenQuery = token.replace("#", "%23");
  var tableIns1;
  var table = layui.table;
  var form = layui.form;
  var layer = layui.layer;
  var $ = jQuery = layui.jquery;
  var laydate = layui.laydate;

  layui.use(['table', 'layer', 'laydate'], function () {
      laydate.render({
          elem: '#startTime', //指定元素
          type: 'datetime',
          format:'yyyy-MM-dd HH:mm:ss'
      });
      laydate.render({
          elem: '#endTime'
          ,type: 'datetime'
          ,format: 'yyyy-MM-dd HH:mm:ss'
      });
    //加载table
    tableIns1 = table.render({
      elem: '#showTable'
      , contentType: 'application/json'
      , headers: {"authorization": token}
      , page: false //开启分页
      , url: ctx + 'report/classActive/listByPage' //数据接口
      , method: 'POST'
      , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
        return {
          "code": res.code, //解析接口状态
          "msg": res.msg, //解析提示文本
          "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
          "data": CoreUtil.isEmpty(res.data) ? null : res.data //解析数据列表
        }
      }
      , cols: [
        [
            {field: 'className', title: '班级', sort: true},
            {field: 'activeNumber', title: '发布活动数量', sort: true},
          // {field: 'classId', title: '班级ID', sort: true},
          // {field: 'typeId', title: '类别ID', sort: true},
            // {field: 'createTime', title: '${column.comments}', sort: true},
            // {field: 'updateTime', title: '${column.comments}', sort: true},
          //{width: 120, toolbar: "#tool", title: '操作'}
        ]
      ]
      , toolbar: '#toolbar'
    });


    //表头工具
    table.on('toolbar(showTable)', function(obj){
      switch(obj.event){
        case 'batchDeleted':
          var checkStatus = table.checkStatus(obj.config.id);
          var data = checkStatus.data;
          if(data.length==0){
            layer.msg("请选择要批量删除的列");
          }else {
            var ids = [];
            $(data).each(function (index,item) {
              ids.push(item.id);
            });
            tipDialog(ids);
          }
          break;
        case 'add':
          $(".table_div").hide();
          $(".operation").show();
          $(".title").html("新增");
          setTimeout(function () {
            form.val('info', {
                "id": "",
              "name": ""
              , "startTime": ""
              , "endTime": ""
              , "desr": ""
              , "amount": ""
              , "classId": ""
              , "typeId": ""
            });
          }, 200);
          break;
      };
    });
    //列操作
    table.on('tool(showTable)',function (obj) {
      var data = obj.data;
      switch (obj.event) {
        case 'del':
          var ids=[];
          ids.push(data.id);
          tipDialog(ids);
          break;
        case 'edit':
          $(".table_div").hide();
          $(".operation").show();
          $(".title").html("编辑");
          setTimeout(function () {
            form.val('info', {
                "id": data.id
                , "name": data.name
                , "startTime": data.startTime
                , "endTime": data.endTime
                , "desr": data.desr
                , "amount": data.amount
                , "classId": data.classId
                , "typeId": data.typeId
            });
          }, 200);
          break;
      }
    });

    //导出
    $('#export').on('click', function () {
      //原先分页limit
      var exportParams = {
        limit: 10000,
        name: $("#searchKeyName").val()
      };
      CoreUtil.sendPost(ctx + "aActive/listByPage", exportParams, function (res) {
        //初始化渲染数据
        if (res.data != null && res.data.records != null) {
          table.exportFile(tableIns1.config.id, res.data.records, 'xls');
        }
      });
    });

    //删除
    var tipDialog=function (ids) {
      layer.open({
        content: "确定要删除么?",
        yes: function(index, layero){
          layer.close(index); //如果设定了yes回调，需进行手工关闭
          CoreUtil.sendDelete(ctx + "aActive/delete",ids,function (res) {
            layer.msg(res.msg, {time:1000},function () {
              searchInfo();
            });
          });
        }
      });
    };

    //返回
    $("#btn_cancel").click(function() {
      $(".table_div").show();
      $(".operation").hide();
      return false;
    });

    //监听保存
    form.on('submit(submit)', function(data){
      if(data.field.id===undefined || data.field.id===null || data.field.id===""){
        CoreUtil.sendPost(ctx + "aActive/add",data.field,function (res) {
          $(".table_div").show();
          $(".operation").hide();
          searchInfo();
        });
      }else {
        CoreUtil.sendPut(ctx + "aActive/update",data.field,function (res) {
          $(".table_div").show();
          $(".operation").hide();
          searchInfo();
        });
      }

      return false;
    });
  });

  //执行查询
  function searchInfo() {
    //这里以搜索为例
    tableIns1.reload({
      where: { //设定异步数据接口的额外参数，任意设
          name: $("#searchKeyName").val()
      }
    });
  };
</script>